package com.xbongbong.paas.help.menu;

import com.xbongbong.admin.pojo.dto.MirrorRollBackDTO;
import com.xbongbong.paas.domain.entity.PaasAppEntity;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.domain.entity.PaasMenuEntity;
import com.xbongbong.paas.model.PaasMenuModel;
import com.xbongbong.paas.pojo.dto.MenuGroupAddDTO;
import com.xbongbong.paas.service.PaasMenuService;
import com.xbongbong.paas.service.init.AbstractInitMenuHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.AccessLinkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.menu.enums.ContractCompareMenuEnum;
import com.xbongbong.pro.enums.menu.enums.SystemAppMenuEnum;
import com.xbongbong.pro.menu.pojo.dto.MenuGroupAddBatchDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuInitDTO;
import com.xbongbong.pro.menu.pojo.vo.MenuGroupAddBatchVO;
import com.xbongbong.pro.menu.pojo.vo.MenuInitVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zhushuainan
 * @title: InitContractComapreMenuHelp
 * @projectName xbb-pro
 * @description: 合同比对菜单初始化
 * @date 2022/11/18 9:35
 */
@Component
public class InitContractComapreMenuHelp extends AbstractInitMenuHelp {

    private static final Logger LOG = LoggerFactory.getLogger(InitCoolAppBundleMenuHelp.class);

    @Resource
    private PaasMenuService paasMenuService;
    @Resource
    private PaasMenuModel paasMenuModel;

    @Override
    public List<String> alias() {
        return Arrays.asList(SystemAppMenuEnum.CONTRACT_COMPARE.getAlias());
    }

    @Override
    public MenuInitVO initApp(MenuInitDTO menuInitDTO, Map<String, PaasAppEntity> appMap) throws XbbException {
        return initMenu(menuInitDTO, appMap);
    }



    @Override
    public MenuInitVO initMenu(MenuInitDTO menuInitDTO, Map<String, PaasAppEntity> appMap) throws XbbException {
        MenuInitVO menuInitVO = new MenuInitVO();
        try {
            // 获取应用id
            Long appId = appMap.get(SystemAppMenuEnum.CONTRACT_COMPARE.getAlias()).getId();
            menuInitDTO.setAppId(appId);
            PaasMenuEntity paasMenuEntity = paasMenuModel.getByAlias(ContractCompareMenuEnum.CONTRACT_COMPARE_RESULT.getAlias(), appId, menuInitDTO.getCorpid());
            if (Objects.nonNull(paasMenuEntity)){
                LOG.error("---------contractCompare menu is already exist");
                return menuInitVO;
            }
            MenuGroupAddBatchVO menuGroupAddBatchVO = groupAddBatch(menuInitDTO);
            if(Objects.isNull(menuGroupAddBatchVO)){
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
            }
        } catch (XbbException e) {
            throw e;
        } catch (Exception e){
            LOG.error("InitContractComapreMenuHelp.menuInit初始化菜单出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return menuInitVO;
    }

    @Override
    public void rollBackInit(MirrorRollBackDTO mirrorRollBackDTO, List<Long> installFormList, PaasFormEntity productFormEntity) throws XbbException {

    }
    private MenuGroupAddBatchVO groupAddBatch(MenuInitDTO menuInitDTO) throws XbbException {
        MenuGroupAddBatchVO menuGroupAddBatchVO;
        try {
            Long appId = menuInitDTO.getAppId();
            MenuGroupAddBatchDTO menuGroupAddBatchDTO = new MenuGroupAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, menuGroupAddBatchDTO);
            menuGroupAddBatchDTO.setAppId(appId);
            List<MenuGroupAddDTO> groupMenuList = new ArrayList<>();
            for(ContractCompareMenuEnum contractCompareMenuEnum: ContractCompareMenuEnum.values()){
                if(StringUtil.isEmpty(contractCompareMenuEnum.getParent())){
                    MenuGroupAddDTO menuGroupAddDTO = new MenuGroupAddDTO();
                    BeanUtil.copyProperties(menuInitDTO, menuGroupAddDTO);
                    menuGroupAddDTO.setParentId(0L);
                    menuGroupAddDTO.setAppId(appId);
                    menuGroupAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                    menuGroupAddDTO.setAlias(contractCompareMenuEnum.getAlias());
                    menuGroupAddDTO.setName(contractCompareMenuEnum.getName());
                    AccessLinkEnum accessLinkEnum = contractCompareMenuEnum.getAccessLinkEnum();
                    menuGroupAddDTO.setIcon(accessLinkEnum.getIcon());
                    menuGroupAddDTO.setColor(accessLinkEnum.getColor());
                    menuGroupAddDTO.setUrl(accessLinkEnum.getUrl());
                    menuGroupAddDTO.setMobileUrl(accessLinkEnum.getMobileUrl());
                    menuGroupAddDTO.setType(contractCompareMenuEnum.getType());
                    groupMenuList.add(menuGroupAddDTO);
                }
            }
            menuGroupAddBatchDTO.setMenuList(groupMenuList);
            menuGroupAddBatchVO = paasMenuService.addBatchGroupMenu(menuGroupAddBatchDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("InitContractComapreMenuHelp.groupAddBatch批量添加分组出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return menuGroupAddBatchVO;
    }
}
